package com.smyh.server;

import com.smyh.UIM.record.DeviceFaultRecordFm;
import com.smyh.UIM.record.DeviceSyncRecordFm;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.protcol.device.WaveDataCacheTemp;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SuperAddRecordPointTread extends Thread {
	public static boolean isStart = false;
	public static long lastRunTime = 0;
	// 是否校验数据
	public static boolean isCheckData = false;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			try {

				if (isCheckData && (System.currentTimeMillis() - lastRunTime) >= 1.5 * 1000) {
					lastRunTime = System.currentTimeMillis();
					isCheckData = false;
					if (WaveDataCacheTemp.resendTime < 5) {
						boolean isRecordFull = false;
						List<Integer> waveDatalist = new ArrayList<>();
						int i = 0;
						byte[] bytes = null;
						if (WaveDataCacheTemp.currentType == (byte) 0xDC
								|| WaveDataCacheTemp.currentType == (byte) 0xDD) {
							// 同步电流同步电压从1开始
							i = 1;
							bytes = new byte[WaveDataCacheTemp.totalPack];
							// 判断波形是否完整
							if (WaveDataCacheTemp.commCurrentMap.size() == WaveDataCacheTemp.totalPack) {
								isRecordFull = true;
							}
						} else {
							bytes = new byte[WaveDataCacheTemp.totalPack + 1];
							// 判断波形是否完整
							if (WaveDataCacheTemp.commCurrentMap.size() == (WaveDataCacheTemp.totalPack + 1)) {
								isRecordFull = true;
							}
						}

						for (; i <= WaveDataCacheTemp.totalPack; i++) {
							if (isRecordFull) {
								if (i == 0) {
									continue;
								}
								// 数据完整。存入list 展示波形
								waveDatalist.addAll(WaveDataCacheTemp.commCurrentMap.get(i));
							} else {
								// 数据不完整，存入数组.补全数据
								if (WaveDataCacheTemp.commCurrentMap.get(i) == null) {
									bytes[i] = 1;
								}
							}
						}
						if (isRecordFull) {
							// 数据完整。展示波形
							switch (WaveDataCacheTemp.currentType) {
								case (byte) 0xDC:
									DeviceSyncRecordFm.syncRecordElecMap.put(MainCahce.chooseDeviceAddr,waveDatalist);
									DeviceSyncRecordFm.freshModel();
									break;
								case (byte) 0xDD:
									DeviceSyncRecordFm.syncRecordVoltMap.put(MainCahce.chooseDeviceAddr,waveDatalist);
									DeviceSyncRecordFm.freshModel();
									break;
								case (byte) 0xDE:
									DeviceFaultRecordFm.faultRecordElecMap.put(MainCahce.chooseDeviceAddr,waveDatalist);
									DeviceFaultRecordFm.freshModel();
									break;
								case (byte) 0xDF:
									DeviceFaultRecordFm.faultRecordVoltMap.put(MainCahce.chooseDeviceAddr,waveDatalist);
									DeviceFaultRecordFm.freshModel();
									break;

							}
							WaveDataCacheTemp.resendTime = 0;
							WaveDataCacheTemp.commCurrentMap = new HashMap<>();
						} else {
							// 数据不完整，补全数据
							WaveDataCacheTemp.resendTime++;
							if(MinaClientThread.session!=null){
								IoBuffer ioBuffer = CMDUtil.superaddRecordPoint(WaveDataCacheTemp.currentType,
										WaveDataCacheTemp.totalPack, bytes);
								MinaClientThread.session.write(ioBuffer);
							}
						}
					} else {
						new Thread(new ToastShowMsgThread("录波补全五次失败")).start();
						WaveDataCacheTemp.resendTime = 0;
						WaveDataCacheTemp.commCurrentMap = new HashMap<>();
					}

				}
			} catch (Exception e) {
				
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}

}
